<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cart;
use App\Order;
use App\Info;
use DB;

/**
*订单相关控制器
*/

class OrderController extends Controller
{
    //加载填写订单页面
    public function orderadd(Request $req){
    	//取出购物车列表页穿过来的cart_id
    	unset($_POST['_token']);
    	$key = array_keys($_POST);

    	//取出提交过来的所有商品，并展示出来
    	$carts = Cart::whereIn('cart_id',$key)->leftjoin('goods','carts.gid','=','goods.gid')->get(['goods.goods_brief','carts.*']);
    	//计算出总金额
    	$totle = 0;
    	foreach($carts as $v){
    		$totle += $v->price*$v->num;
    	}

    	//将商品写入session
    	$req->session()->put('carts',$carts);

    	return view('flow.myorder',['carts'=>$carts,'totle'=>$totle]);
    }

    public function ordercom(Request $r){
    	$order = new Order();
    	$info = new Info();
    	$uid = session('uid');

    	//取出session中的商品
    	$carts = $r->session()->get('carts');

    	//将信息写入order表
    	$order->uid = $uid;
    	$order->price = $r->totle;
    	$order->name_ = $r->name;
    	$order->address = $r->address;
    	$order->mobile_ = $r->mobile;
    	$order->status = 0;
        $order->xdtime = time();
        $order->order_num = date('YmdHis').mt_rand(1000,9999);

        $res = $order->save();
    	if(!$res){
    		return back();
    	}

    	//将信息写入info表格
    	$row = array(
            'oid'=>$order->order_id
            );
    	foreach($carts as $v){
            $row['gid'] = $v->gid;
            $row['curr_price'] = $v->price;
    		$row['num'] = $v->num;
    		DB::table('infos')->insert($row);

            //删除购物车中该条数据
            DB::table('carts')->where('gid',$v->gid)->where('uid',$uid)->delete();
    	}

        //http提交数据到第三方支付平台
        $info = array(
            'v_mid'=>'20272562',
            'v_oid'=>$order->order_num,
            'v_amount'=>$order->price,
            'v_moneytype'=>'CNY',
            'v_url'=>'http://ooo.com/home/pay/'.$order->order_id,
            'v_key'=>'%()#QOKFDLS:1*&U',
            );
         $info['v_md5info']=strtoupper(md5($info['v_amount'].$info['v_moneytype'].$info['v_oid'].$info['v_mid'].$info['v_url'].$info['v_key']));
        
        return view('flow.pay',$info);

    }

    //处理支付页面穿回的信息
    public function pay(Request $r,$oid){
        if($r->v_pstatus != 20){
            return '支付失败';
        }
        $key = '%()#QOKFDLS:1*&U';
        $md5str = strtoupper(md5($r->v_oid.$r->v_pstatus.$r->v_amount.$r->v_moneytype.$key));
        if($md5str !== $r->v_md5str){
            return '信息错误';
        }
        $order = Order::where('order_id',$oid)->first();
        $order->status = 1;
        $order->fktime = time();
        $order->save();
        return redirect('home/orderlist');
    }

    //home页面的订单列表
    public function orderlist(){
        $uid = session('uid');
        $orders = Order::where('uid',$uid)->get();
        foreach ($orders as $key => $value) {
            $orders[$key]['gid'] = Info::where('oid',$value->order_id)->leftjoin('goods','infos.gid','=','goods.gid')->get(['goods.goods_name','goods_brief','thumb_img','infos.curr_price','infos.num']);
        }
        //dd($orders);
        return view('flow.buyed',['orders'=>$orders]);
    }
}
